package com.southwind.dao.impl;

import com.southwind.dao.DormitoryAdminDao;
import com.southwind.entity.DormitoryAdmin;
import com.southwind.entity.SystemAdmin;
import com.southwind.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DormitoryAdminDaoImpl implements DormitoryAdminDao {
    @Override
    public List<DormitoryAdmin> list() {
        Connection con = JDBCUtil.getConnection();
        PreparedStatement stmt = null;
        ResultSet res = null;
        String sql = "select * from dormitory_admin";
        List<DormitoryAdmin> list = new ArrayList<>();
        try {
            stmt = con.prepareStatement(sql);
            res = stmt.executeQuery();
            while(res.next())
            {
                int id = res.getInt(1);
                String uname = res.getString(2);
                String password = res.getString(3);
                String name = res.getString(4);
                String gender = res.getString(5);
                String phone = res.getString(6);
                list.add(new DormitoryAdmin(id, uname, password, name, gender, phone));
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.release(con,stmt,res);
        }

        return list;
    }

    @Override
    public List<DormitoryAdmin> search(String key, String value) {
        Connection con = JDBCUtil.getConnection();
        PreparedStatement stmt = null;
        ResultSet res = null;
        String sql = "select * from dormitory_admin where "+key+" like '%"+value+"%'";
        List<DormitoryAdmin> list = new ArrayList<>();
        try {
            stmt = con.prepareStatement(sql);
            res = stmt.executeQuery();
            while(res.next())
            {
                int id = res.getInt(1);
                String uname = res.getString(2);
                String password = res.getString(3);
                String name = res.getString(4);
                String gender = res.getString(5);
                String phone = res.getString(6);
                list.add(new DormitoryAdmin(id, uname, password, name, gender, phone));
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.release(con,stmt,res);
        }

        return list;
    }

    @Override
    public Integer save(DormitoryAdmin dormitoryAdmin) {
        Connection con = JDBCUtil.getConnection();
        PreparedStatement stmt = null;
        Integer result = null;
        String sql = "insert into dormitory_admin(username, password, name, gender, telephone) value(?,?,?,?,?)";
        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, dormitoryAdmin.getUsername());
            stmt.setString(2, dormitoryAdmin.getPassword());
            stmt.setString(3, dormitoryAdmin.getName());
            stmt.setString(4, dormitoryAdmin.getGender());
            stmt.setString(5, dormitoryAdmin.getTelephone());
            result = stmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.release(con,stmt, null);
        }

        return result;
    }

    @Override
    public Integer update(DormitoryAdmin dormitoryAdmin) {
        Connection con = JDBCUtil.getConnection();
        PreparedStatement stmt = null;
        Integer result = null;
        String sql = "update dormitory_admin set username=?,password=?,name=?,gender=?,telephone=? where id=? ";
        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, dormitoryAdmin.getUsername());
            stmt.setString(2, dormitoryAdmin.getPassword());
            stmt.setString(3, dormitoryAdmin.getName());
            stmt.setString(4, dormitoryAdmin.getGender());
            stmt.setString(5, dormitoryAdmin.getTelephone());
            stmt.setInt(6, dormitoryAdmin.getId());
            result = stmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.release(con,stmt, null);
        }

        return result;
    }

    @Override
    public Integer delete(Integer id) {
        Connection con = JDBCUtil.getConnection();
        PreparedStatement stmt = null;
        Integer result = null;
        String sql = "delete from dormitory_admin where id=" + id;
        try {
            stmt = con.prepareStatement(sql);
            result = stmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.release(con,stmt, null);
        }

        return result;
    }

    @Override
    public DormitoryAdmin findByUsername(String username) {
        Connection con = JDBCUtil.getConnection();
        PreparedStatement stmt = null;
        ResultSet res = null;
        String sql = "select * from dormitory_admin where username = ?";
        DormitoryAdmin dormitoryAdmin = null;
        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, username);
            res = stmt.executeQuery();
            while(res.next())
            {
                Integer id = res.getInt(1);
                String uname = res.getString(2);
                String password = res.getString(3);
                String name = res.getString(4);
                dormitoryAdmin = new DormitoryAdmin(id, uname, password, name);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.release(con,stmt,res);
        }

        return dormitoryAdmin;
    }
}
